package com.shijie99.demo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springmore.core.datasource.DataSourceHolder;

import com.shijie99.demo.dao.ConfigCookieMapper;
import com.shijie99.demo.dao.ConfigUserMapper;
import com.shijie99.demo.pojo.ConfigCookie;
import com.shijie99.demo.pojo.ConfigCookieExample;
import com.shijie99.demo.pojo.ConfigUser;
import com.shijie99.demo.pojo.ConfigUserExample;
import com.shijie99.demo.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	private ConfigUserMapper configUserMapper;
	
	@Autowired
	private ConfigCookieMapper configCookieMapper;
	
	@Override
	public void insertConfigCookie(ConfigCookie configCookie) {
		configCookieMapper.insertSelective(configCookie);
	}

	@Override
	public ConfigCookie selectConfigCookieByExample(ConfigCookieExample example) {
		List<ConfigCookie> cookiesList = configCookieMapper.selectByExample(example);
		if(cookiesList != null && cookiesList.size() > 0) {
			return cookiesList.get(0);
		} else {
			return null;
		}
	}
	
	@Override
	public void deleteConfigCookieByExample(ConfigCookieExample example) {
		configCookieMapper.deleteByExample(example);
	}

	@Override
	public ConfigUser selectConfigUserByPriamryKey(int id) {
		return configUserMapper.selectByPrimaryKey(id);
	}

	@Override
	public ConfigUser authConfigUserByExample(ConfigUserExample example) {
		List<ConfigUser> configUsers = configUserMapper.selectByExample(example);
		if(configUsers != null && configUsers.size() > 0) {
			return configUsers.get(0);
		} else {
			return null;
		}
	}

	@Override
	@Transactional(readOnly=false)
	public ConfigUser test() {
		System.out.println(DataSourceHolder.isMaster());
		int row = configCookieMapper.countByExample(null);
		System.out.println(row);
		ConfigUser user = new ConfigUser();
		user.setName("---");
		user.setPassword("111");
		configUserMapper.insertSelective(user);
		return user;
	}
	
	@Override
	@Transactional(readOnly=true)
	public int test1() {
		System.out.println(DataSourceHolder.isMaster());
		int row = configCookieMapper.countByExample(null);
		System.out.println(row);
		row = configUserMapper.countByExample(null);
		return row;
	}
}
